// 开发模式下的临时认证服务
import { User, LoginCredentials, RegisterData } from '@/types/auth';
import { TauriApiResponse } from '@/types/api';

export const devAuthService = {
  async login(credentials: LoginCredentials): Promise<TauriApiResponse<User>> {
    // 开发模式下接受任何有效邮箱和密码
    if (!credentials.email || !credentials.password) {
      return {
        success: false,
        error: '邮箱和密码不能为空',
      };
    }

    if (credentials.password.length < 6) {
      return {
        success: false,
        error: '密码至少需要6个字符',
      };
    }

    // 开发模式下的默认用户
    const devUser: User = {
      id: 'dev-user-001',
      username: '开发用户',
      email: credentials.email,
      role: 'admin',
      avatar: undefined,
      isEmailVerified: true,
      preferences: {
        theme: 'system',
        language: 'zh-CN',
        timezone: 'Asia/Shanghai',
        notifications: {
          email: true,
          desktop: true,
          taskCompletion: true,
          systemUpdates: true,
          marketing: false,
        },
        privacy: {
          profileVisibility: 'public',
          activityTracking: true,
          analyticsData: true,
        },
      },
      createdAt: new Date(),
      updatedAt: new Date(),
      subscription: {
        id: 'sub-' + Date.now(),
        plan: 'pro',
        status: 'active',
        startDate: new Date(),
        endDate: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // 1年后
        autoRenew: true,
        features: ['unlimited-processing', 'priority-support', 'advanced-export'],
        usage: {
          videosProcessed: 0,
          storageUsed: 0,
          monthlyLimit: 1000,
          storageLimit: 50 * 1024 * 1024 * 1024, // 50GB
        },
      },
    };

    // 存储到localStorage
    localStorage.setItem('auth_token', 'dev-token-' + Date.now());
    localStorage.setItem('auth_user', JSON.stringify(devUser));

    return {
      success: true,
      data: devUser,
    };
  },

  async register(data: RegisterData): Promise<TauriApiResponse<User>> {
    // 开发模式下的注册
    const devUser: User = {
      id: 'dev-user-' + Date.now(),
      username: data.username || data.email.split('@')[0],
      email: data.email,
      role: 'user',
      avatar: undefined,
      isEmailVerified: false,
      preferences: {
        theme: 'system',
        language: 'zh-CN',
        timezone: 'Asia/Shanghai',
        notifications: {
          email: true,
          desktop: true,
          taskCompletion: true,
          systemUpdates: true,
          marketing: false,
        },
        privacy: {
          profileVisibility: 'private',
          activityTracking: true,
          analyticsData: true,
        },
      },
      createdAt: new Date(),
      updatedAt: new Date(),
      subscription: {
        id: 'sub-' + Date.now(),
        plan: 'free',
        status: 'active',
        startDate: new Date(),
        endDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 30天后
        autoRenew: false,
        features: ['basic-processing', 'standard-export'],
        usage: {
          videosProcessed: 0,
          storageUsed: 0,
          monthlyLimit: 10,
          storageLimit: 5 * 1024 * 1024 * 1024, // 5GB
        },
      },
    };

    localStorage.setItem('auth_token', 'dev-token-' + Date.now());
    localStorage.setItem('auth_user', JSON.stringify(devUser));

    return {
      success: true,
      data: devUser,
    };
  },

  async logout(): Promise<TauriApiResponse<void>> {
    localStorage.removeItem('auth_token');
    localStorage.removeItem('auth_user');
    return { success: true };
  },

  async getCurrentUser(): Promise<TauriApiResponse<User>> {
    const userStr = localStorage.getItem('auth_user');
    if (!userStr) {
      return {
        success: false,
        error: '用户未登录',
      };
    }

    try {
      const user = JSON.parse(userStr);
      return { success: true, data: user };
    } catch {
      return {
        success: false,
        error: '用户信息解析失败',
      };
    }
  },
};

// 替换原来的认证服务
const isDevelopment = process.env.NODE_ENV === 'development';
export const authService = isDevelopment ? devAuthService : devAuthService; // 暂时都使用开发模式
